import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * user:likai
 * Date:2022-12-01
 * Time:19:58
 */
// key-value 模型
class HashBucket {
    private static class Node {
        private int key;
        private int value;
        Node next;


        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }


    private Node[]  array;
    private int size;   // 当前的数据个数
    private static final double LOAD_FACTOR = 0.75;
    private static final int DEFAULT_SIZE = 8;//默认桶的大小
    public HashBucket() {
        // write code here
        array=new Node[8];
    }

    public int put(int key, int value) {
        // write code here
        int index=key%array.length;
        for(Node cur=array[index];cur.next!=null;cur=cur.next){
            if(key==cur.key){
                int old=cur.value;
                cur.value=value;
                return old;
            }
        }
        Node node=new Node(key,value);
        node.next=array[index];
        array[index]=node;
        size++;
        //扩容
        if(loadFactor()>=0.75){
            resize();
        }
        return -1;
    }


    private void resize() {
        // write code here
        Node[] arr=new Node[array.length*2];
        for (int i = 0; i < array.length; i++) {
            Node next;
            for(Node node=array[i];node!=null;node=next){
                next=node.next;
                int index=node.key%arr.length;
                node.next=arr[index];
                arr[index]=node;
            }
        }
        array=arr;
    }


    private double loadFactor() {
        return size * 1.0 / array.length;
    }





    public int get(int key) {
        // write code here
        int index=key%array.length;
        for (Node cur=array[index]; cur!=null; cur=cur.next) {
            if(key==cur.key){
                return cur.value;
            }
        }
        return -1;
    }
}
public class Test {

}
